{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_boston\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, RidgeCV\n",
    "from sklearn.metrics import mean_squared_error  # 均方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [[80, 86],\n",
    "[82, 80],\n",
    "[85, 78],\n",
    "[90, 90],\n",
    "[86, 82],\n",
    "[82, 90],\n",
    "[78, 80],\n",
    "[92, 94]]\n",
    "y = [84.2, 80.6, 80.1, 90, 83.2, 87.6, 79.4, 93.4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=2, test_size=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# transfer = StandardScaler()\n",
    "\n",
    "# x_train = transfer.fit_transform(x_train)\n",
    "# x_test = transfer.fit_transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression()"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "estimator = LinearRegression()  # fit_intercept默认为True，计算偏置\n",
    "\n",
    "# 线性回归没有交叉验证-网格搜索\n",
    "# param_grid = {}\n",
    "# estimator = GridSearchCV(estimator,param_grid=param_grid, cv=5)\n",
    "\n",
    "estimator.fit(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.3, 0.7])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "estimator.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.4210854715202004e-14"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "estimator.intercept_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 波士顿房价预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 获取数据\n",
    "data = load_boston()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 数据分割\n",
    "x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, random_state=2, test_size=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 特征工程\n",
    "# 3.1 实例化标准化转换器\n",
    "transfer = StandardScaler()\n",
    "\n",
    "# 3.2 数据标准化\n",
    "x_train = transfer.fit_transform(x_train)\n",
    "x_test = transfer.fit_transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RidgeCV(alphas=array([1.e-04, 1.e-03, 1.e-01, 1.e-02, 1.e+00, 1.e+01, 1.e+02]))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4. 机器学习\n",
    "# # 4.1.1 创建随机梯度下降估计器\n",
    "# estimator = SGDRegressor(max_iter=1000)\n",
    "# # 4.1.2 创建正规方程估计器\n",
    "# estimator = LinearRegression()\n",
    "# # 4.1.3 创建岭回归估计器\n",
    "# estimator = Ridge()\n",
    "# 4.1.4 创建可对alpha进行交叉验证的岭回归估计器\n",
    "estimator = RidgeCV(alphas=(0.0001, 0.001, 0.1, 0.01, 1, 10, 100))\n",
    "\n",
    "# 4.2 训练模型\n",
    "estimator.fit(x_train, y_train)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "估计目标值和测试目标值对比:\n",
      " [False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False False False False False False False\n",
      " False False False False False False]\n",
      "模型准确率:\n",
      " 0.7727320206989629\n",
      "偏置为:\n",
      " 22.44133663366339\n",
      "回归系数为:\n",
      " [-0.86329633  0.7285083  -0.27135102  0.85108307 -1.63780795  2.6270911\n",
      "  0.18222203 -2.64613645  2.17038535 -1.42056563 -2.05032997  1.07266175\n",
      " -3.76668388]\n",
      "均方差为:\n",
      " 19.013166843729284\n"
     ]
    }
   ],
   "source": [
    "# 5. 模型评估\n",
    "# 5.1 估计目标值\n",
    "y_pre = estimator.predict(x_test)\n",
    "print(\"估计目标值和测试目标值对比:\\n\", y_pre == y_test)\n",
    "\n",
    "# 5.2 模型准确率\n",
    "print(\"模型准确率:\\n\", estimator.score(x_test, y_test))\n",
    "\n",
    "# 5.3 偏置\n",
    "print(\"偏置为:\\n\", estimator.intercept_)\n",
    "\n",
    "# 5.4 回归系数\n",
    "print(\"回归系数为:\\n\", estimator.coef_)\n",
    "\n",
    "# 5.5 均方差：\n",
    "print(\"均方差为:\\n\", mean_squared_error(y_test, y_pre))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
